﻿using InternetSpiderLib;
using InternetSpiderLib.Tools;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WeChatGZHTextExtractor.DB;
using WeChatGZHTextExtractor.WebModel;

namespace WeChatGZHTextExtractor.BusinessCreaters
{
    /// <summary>
    /// 发送数据
    /// </summary>
    public class BC_SendData : IBusinessCreater
    {
        private string APIUrl;

        public BC_SendData()
        {
            APIUrl = SingleCase.Get<XmlConfig>().Get<string>("SendDataAPI");
        }

        public Action Create()
        {
            var system_data_service = new SystemDataService();
            var send_status = system_data_service.Get<string>(PublicKey.SystemData.SEND_STATUS);

            if (send_status == PublicKey.SystemData.SEND_STATUS_SENDING)
                return null;

            var send_index = system_data_service.Get<long>(PublicKey.SystemData.SEND_DATA_INDEX);

            var composition_service = new BaseService<WXCompositionModel>();
            var composition_model = composition_service.Query(context =>
            {
                return context
                    .Where(n => n.Id > send_index)
                    .Where(n => n.IsFinsh)
                    .FirstOrDefault();
            });

            if (composition_model == null)
                return null;

            system_data_service.Set<string>(PublicKey.SystemData.SEND_STATUS, PublicKey.SystemData.SEND_STATUS_SENDING);
            system_data_service.Set<long>(PublicKey.SystemData.SEND_DATA_INDEX, composition_model.Id);

            return () =>
            {
                var account_service = new BaseService<WXAccountModel>();
                var account_model = account_service.Query(context => context.FirstOrDefault(n => n.Id == composition_model.WXAccountId));

                LogHelper.GetInstance().Log(LogType.Info, $"Send Data {JsonConvert.SerializeObject(composition_model)}");

                DecorationRaidersModel raiders_model = new DecorationRaidersModel
                {
                    Author = account_model?.WXAccount ?? string.Empty,
                    Body = composition_model.RichMediaContent,
                    CoverPath = composition_model.Thumbnail,
                    SourceUrl = composition_model.Url,
                    Summary = composition_model.Describe,
                    Title = composition_model.Title,
                };

                var back_data = string.Empty;
                JObject back_obj = null;
                try
                {
                    back_data = HttpHelper.HttpPost(APIUrl, new { model = raiders_model });
                    back_obj = (JObject)JsonConvert.DeserializeObject(back_data);
                }
                catch
                {

                }


                if (back_obj == null || !back_obj["Success"].ToObject<bool>())
                {
                    Console.WriteLine($"出错了 {back_data}");
                }

                system_data_service.Set<string>(PublicKey.SystemData.SEND_STATUS, PublicKey.SystemData.SEND_STATUS_WAITING);
            };
        }
    }
}
